package net.gagnat.android.trackit.activity;

import java.util.List;

import net.gagnat.android.trackit.R;
import net.gagnat.android.trackit.domain.LogEntry;
import net.gagnat.android.trackit.service.LogEntriesRepository;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ViewLogEntries extends ListActivity {

	private static final int MENU_NEW_LOG_ENTRY = 0;
	private static final int MENU_VIEW_ALL_TAGS = 1;

	private static final int CONTEXT_MENU_DELETE = 0;
	private static final int CONTEXT_MENU_EDIT = 1;
	private ArrayAdapter<LogEntry> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogEntriesRepository.initialize(this);

		setContentView(R.layout.trackit_list);
		registerForContextMenu(getListView());
	}

	@Override
	protected void onResume() {
		super.onResume();

		List<LogEntry> logEntries = LogEntriesRepository.getInstance().getLogEntries();
		adapter = new ArrayAdapter<LogEntry>(this, R.layout.trackit_list_entry, R.id.titleEntry, logEntries);
		setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_NEW_LOG_ENTRY, 0, getText(R.string.menu_new_log_entry)); //
		// .setIcon(android.R.drawable.ic_menu_add);
		menu.add(0, MENU_VIEW_ALL_TAGS, 0, getText(R.string.menu_view_all_tags));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_NEW_LOG_ENTRY:
			startActivity(createNewLogEntry());
			return true;
		case MENU_VIEW_ALL_TAGS:
			startActivity(viewAllTags());
		default:
			return false;
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, CONTEXT_MENU_EDIT, 0, getText(R.string.menu_edit));
		menu.add(0, CONTEXT_MENU_DELETE, 0, getText(R.string.menu_delete));
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		LogEntry logEntry = LogEntriesRepository.getInstance().getLogEntry(info.position);
		switch (item.getItemId()) {
		case CONTEXT_MENU_EDIT:
			startActivity(editLogEntry(logEntry.getId()));
			return true;
		case CONTEXT_MENU_DELETE:
			LogEntriesRepository.getInstance().removeLogEntry(logEntry);
			adapter.notifyDataSetChanged();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	private Intent createNewLogEntry() {
		Intent myIntent = new Intent(getBaseContext(), EditLogEntry.class);
		myIntent.setAction(Intent.ACTION_INSERT);
		return myIntent;
	}

	private Intent viewAllTags() {
		return new Intent(getBaseContext(), ViewLogTags.class);
	}

	private Intent editLogEntry(Long id) {
		Intent myIntent = new Intent(getBaseContext(), EditLogEntry.class);
		myIntent.setAction(Intent.ACTION_EDIT);
		myIntent.putExtra(EditLogEntry.NAME_INTENT_EXTRA_LOG_ENTRY_ID, id);
		return myIntent;
	}

}
